package com.boot.study.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.keygen.DefaultKeyGenerator;
import com.google.common.collect.Lists;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Title:
* @Description:
* @author chenchen
* @date 2018/8/6 11:25
*/
@ConditionalOnProperty(prefix = "master-slave", name="open", havingValue ="false", matchIfMissing = true)
@Configuration
public class DbConfig2 {
    @Bean
    public DataSource dataSource() {
        Map<String, DataSource> dataSourceMap = new HashMap<>(2);
        dataSourceMap.put("db_0", createDataSource("db_0"));
        dataSourceMap.put("db_1", createDataSource("db_1"));

        DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);

        TableRule orderTableRule = TableRule.builder("t_order").generateKeyColumn("orderId", DefaultKeyGenerator.class)
                .actualTables(Arrays.asList("t_order_0", "t_order_1", "t_order_2")).dataSourceRule(dataSourceRule)
                .build();

        ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule)
                .tableRules(Arrays.asList(orderTableRule))
                .databaseShardingStrategy(new DatabaseShardingStrategy("userId", new ModuloDatabaseShardingAlgorithm()))
                .tableShardingStrategy(new TableShardingStrategy("orderId", new ModuloTableShardingAlgorithm()))
                .build();

      /*  ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule)
                .tableRules(Arrays.asList(orderTableRule))
                .databaseShardingStrategy(new DatabaseShardingStrategy("userId", new ModuloDatabaseShardingAlgorithm()))
                .tableShardingStrategy(new TableShardingStrategy(Lists.newArrayList("userId", "orderId"), new MultipleKeysModuloTableShardingAlgorithm()))
                .build();*/

        try {
            DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);

            return dataSource;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    private static DataSource createDataSource(final String dataSourceName) {
        DruidDataSource result = new DruidDataSource();
        result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
        result.setUrl(String.format("jdbc:mysql://localhost:3306/%s", dataSourceName));
        result.setUsername("root");
        result.setPassword("111111");
        return result;
    }
}
